package com.example.chartroom1.controller;

import com.example.chartroom1.mapper.MessageMapper;
import com.example.chartroom1.mapper.MessageSessionMapper;
import com.example.chartroom1.model.Friend;
import com.example.chartroom1.model.MessageSession;
import com.example.chartroom1.model.MessageSessionUser;
import com.example.chartroom1.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/session")
public class SessionController {
    @Resource
    private MessageSessionMapper mapper;

    @Resource
    private MessageMapper messageMapper;
    //获取会话列表
    @RequestMapping("/sessionList")
    public Object getSessionList(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session == null){
            log.info("获取session失败");
            return new ArrayList<MessageSession>();
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            log.info("获取user失败");
            return new ArrayList<MessageSession>();
        }

        List<Integer> res = mapper.selectSessionIdByUserId(user.getUserId());
        List<MessageSession> ans = new ArrayList<>();
        for(int sessionId : res){
            MessageSession messageSession = new MessageSession();
            List<Friend> t = mapper.selectFriendsBySessionId(sessionId, user.getUserId());

            messageSession.setSessionId(sessionId);
            messageSession.setFriends(t);
            String lastMessage = messageMapper.getLastMessageBySessionId(sessionId);
            if(lastMessage == null){
                lastMessage = "";
            }
            messageSession.setLastMessage(lastMessage);
            ans.add(messageSession);
        }
        return ans;
    }
    //增加一个会话列表
    @RequestMapping("/createSession")
    @Transactional
    public Object addMessageSession(int toUserId, @SessionAttribute("user") User user){
        MessageSession messageSession = new MessageSession();
        mapper.addMessageSession(messageSession);

        MessageSessionUser user1 = new MessageSessionUser();
        user1.setUserId(user.getUserId());
        user1.setSessionId(messageSession.getSessionId());
        mapper.addMessageSessionUser(user1);

        MessageSessionUser user2 = new MessageSessionUser();
        user2.setSessionId(messageSession.getSessionId());
        user2.setUserId(toUserId);
        mapper.addMessageSessionUser(user2);

        Map<String, Integer> map = new HashMap<>();
        map.put("sessionId", messageSession.getSessionId());
        return map;
    }
}
